const multer = require('multer');
const fs = require('fs');
const storage = multer.diskStorage({
    destination: function (req, file, cb) {
        //定义一个动态目录
        let s = 'public/'
        const data = new Date();
        s += data.getFullYear() + '/';
        s += (data.getMonth() + 1) + '/';
        s += data.getDate() + '/';
        // { recursive: ture }表示允许mkdir一次创建多级目录
        fs.mkdirSync(s, { recursive: true })
        cb(null, s);
    },
    filename: function (req, file, cb) {
        //文件名的指定，要求文件名越长越好，目的是避免重复
        const data = new Date();
        let s = '';
        s += data.getFullYear();
        s += ("0" + (data.getMonth() + 1)).slice(-2);
        s += ('0' + data.getDate()).slice(-2);
        s += ('0' + data.getHours()).slice(-2);
        s += ('0' + data.getMinutes()).slice(-2);
        s += ('0' + data.getSeconds()).slice(-2);
        s += ('00' + data.getMilliseconds()).slice(-3);
        s += Math.floor(Math.random() * 1000);
        const arr = file.originalname.split('.');
        s += "." + arr[arr.length - 1]
        cb(null, s)

    }
});
const upload = multer({
    storage,

});
module.exports = upload